package com.itcast;

import bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/listStudent")
public class ListStudent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\stu.txt"));
        ArrayList<Student> arrayList = new ArrayList<>();
        String len;
        while ((len=br.readLine())!=null){
            String [] arr = len.split(",");
            Student s = new Student(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]));
            arrayList.add(s);
        }
         //将集合对象存入会话域中
        req.getSession().setAttribute("student",arrayList);
        //重定向学生列表
        resp.sendRedirect("/stu/listStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
